package cool.tianyou.blue.bird.common.tool;

import cool.tianyou.blue.bird.common.model.po.LoginUser;
import cool.tianyou.blue.bird.common.util.Assert;
import org.springframework.beans.BeanUtils;

/**
 * @author Liutianyou
 * @date 2021/7/31 11:42 上午
 */
public class UserHolder {

  public static final ThreadLocal<LoginUser> THREAD_LOCAL_USER = new ThreadLocal<>();

  public static void setUser(LoginUser user) {
    THREAD_LOCAL_USER.set(user);
  }

  public static LoginUser getLoginUser() {
    LoginUser loginUser = new LoginUser();
    Assert.isAuth(THREAD_LOCAL_USER.get() != null, "请登录");
    BeanUtils.copyProperties(THREAD_LOCAL_USER.get(), loginUser);
    return loginUser;
  }

  public static void close() {
    THREAD_LOCAL_USER.remove();
  }

}
